﻿
using Jmit.Common;
using Jmit.Deyi.Job;
using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace Jmit.Deyi.AutoTask.DataSync
{
    public partial class Form1 : Form
    {

        public bool isRunning = false;          // 定时任务是否运行
        public static bool isSelectedJob1 = true;     // 是否允许执行任务1
        public static bool isSelectedJob2 = true;     // 是否允许执行任务2

        public Form1()
        {

            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            initConfig();

            //Start();
            UpdateUI(true);
            jobStart = new JobStart();
            jobStart.Start();

            this.notifyIcon1.Visible = true; //在通知区显示Form的Icon
        }


        //private DeyiConfig deyiConfig = new DeyiConfig();

        private void initConfig()
        {

            AppConfigManager.LoadConfig();

            this.tb_down_interval.Value = int.Parse(AppConfigManager.appConfig.down_minute);
            this.tb_up_interval.Value = int.Parse(AppConfigManager.appConfig.up_minute);

            string msg = "";
            if (AppManage.IsNewAccount())
            {
                msg = "(新账号：" + AppManage.newFromName + ")";
            }
            this.toolStripStatusLabel1.Text =  AppManage.appVersion + msg;
            
            LogHelpter.AddLog("是否新开账号：" + AppManage.IsNewAccount());
            LogHelpter.AddLog("版本号：" + AppManage.appVersion + msg);

        }



        private void btnStart_Click(object sender, EventArgs e)
        {
            Start();

            // test
            /*string from_name = "8001B5 丽珠集团23";
            string from_id = AppManage.GetFromDict(from_name);
            string abc = "";*/

        }

        private static JobStart jobStart;
        private void Start()
        {
            //
            isSelectedJob1 = checkBox1.Checked;
            isSelectedJob2 = checkBox2.Checked;

            saveConfig();
            UpdateUI(true);
            TestAsyncStart();

            //if (!isRunning)
            //{
            //    //saveConfig();
            //    UpdateUI(true);
            //    //MainAsyncStart();
            //    TestAsyncStart();


            //}
            //else
            //{
            //    UpdateUI(false);
            //    //MainAsyncShutdown();
            //    Task task1 = TestAsyncShutdown();
            //}

        }

        private void UpdateUI(bool state)
        {

            isRunning = true;
            label1.Text = "运行中...";
            label1.ForeColor = Color.Green;



            //if (state)
            //{
            //    isRunning = true;
            //    label1.Text = "运行中...";
            //    label1.ForeColor = Color.Green;

            //    //btnStart.Text = "停止";

            //    groupBox1.Enabled = false;
            //    groupBox3.Enabled = false;
            //}
            //else
            //{
            //    isRunning = false;
            //    label1.Text = "停止";
            //    label1.ForeColor = Color.Red;

            //    //btnStart.Text = "启动";
            //    groupBox1.Enabled = true;
            //    groupBox3.Enabled = true;
            //}

        }

        #region 定时任务
        static async Task TestAsyncStart()
        {

            //obStart = new JobStart();
            jobStart.Start();
            //jobStart.ReStart();
        }

        static async Task TestAsyncShutdown()
        {
            jobStart.Pause(); // 关闭
            //jobStart.Close(); // 关闭
        }


        #endregion


        #region 托盘最小化和退出



        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //this.Hide();
            if (!isClose)
            {
                this.WindowState = FormWindowState.Minimized;
                notifyIcon1.BalloonTipTitle = this.Text + "提示";
                notifyIcon1.BalloonTipText = "程序将在后台继续为您服务...";
                notifyIcon1.ShowBalloonTip(5000);
                e.Cancel = true;
            }
        }

        bool isClose = false;

        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                contextMenuStrip1.Show();
            }
        }


        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.Visible)
            {
                this.WindowState = FormWindowState.Minimized;
                this.notifyIcon1.Visible = true;
                this.Hide();
            }
            else
            {
                this.Visible = true;
                this.WindowState = FormWindowState.Normal;
                this.Activate();
            }
        }

        /// <summary>
        /// 显示窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
        }

        /// <summary>
        /// 退出服务程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("现在退出程序吗？", "确认退出", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
            if (dr == DialogResult.Yes)
            {
                //try { Log.Info(G_VAR.LogPATH, "程序退出", "退出程序完成"); }
                //catch { ;}
                //Log.WriterLog("退出", "程序被强制关闭.");

                //   在应用程序关闭时运行的代码
                /*var scheduler = schedulerFactory.GetScheduler();
                if (scheduler != null)
                {
                    scheduler.Shutdown(true);
                }*/
                //MainAsyncShutdown();
                jobStart.Close();

                isClose = true;
                Application.Exit();
            }
        }

        #endregion

        private void button2_Click(object sender, EventArgs e)
        {
            saveConfig();

            // MessageBox.Show(this, "保存成功", "提示");
        }

        private void saveConfig()
        {
            AppConfigManager.appConfig.down_minute = this.tb_down_interval.Value.ToString();
            AppConfigManager.appConfig.up_minute = this.tb_up_interval.Value.ToString();
            AppConfigManager.SaveConfig(AppConfigManager.appConfig);
            AppConfigManager.LoadConfig();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            isSelectedJob1 = checkBox1.Checked;
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            isSelectedJob2 = checkBox2.Checked;

        }

        // 退出
        private void button2_Click_1(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("现在退出程序吗？", "确认退出", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
            if (dr == DialogResult.Yes)
            {
                //try { Log.Info(G_VAR.LogPATH, "程序退出", "退出程序完成"); }
                //catch { ;}
                //Log.WriterLog("退出", "程序被强制关闭.");

                //   在应用程序关闭时运行的代码
                /*var scheduler = schedulerFactory.GetScheduler();
                if (scheduler != null)
                {
                    scheduler.Shutdown(true);
                }*/
                //MainAsyncShutdown();
                jobStart.Close();

                isClose = true;
                Application.Exit();
            }
        }
    }
}
